﻿/*
Copyright (C) 2009  Tomasz Chrzanowski

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Windows.Forms;
using Com.Tom.CdcCdcCommon;

namespace Com.TomCdc.ChangeDataCapture
{
    static class Program
    {
        /// <summary>
        /// Create a logger for use in this class.
        /// </summary>
        private static readonly log4net.ILog log =
            log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            log.Info("Application TomCdc Viewer started.");
            // adds the even handler for handling UI thread exceptions
            Application.ThreadException += 
                new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            // adds the even handler for handling non-UI thread exceptions
            AppDomain.CurrentDomain.UnhandledException += 
                new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new SplashScreen(SplashType.Viewer, 3));
            Application.Run(new MainForm());
            log.Info("Application terminated.");
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            log.Error("Non UI unhandled exception.", e.ExceptionObject as Exception);
            FatalErrorMessage(((Exception)e.ExceptionObject).Message );
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            log.Error("UI unhandled exception.", e.Exception);
            FatalErrorMessage(e.Exception.Message);
            Application.Exit();
        }

        static void FatalErrorMessage(string exeptionMsg)
        {
            MessageBox.Show("The Change Data Capture Viewer encountered an exceptional application state, " 
                + Environment.NewLine + "which is not handled, and can not be recovered." + Environment.NewLine 
                + "Appliaction must be closed. Sorry for inconvenience." + Environment.NewLine + Environment.NewLine
                + "Details:" + Environment.NewLine + exeptionMsg,                
                "Appliaction Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
